package com.lcl.jarvis.admin.modules.person.service.impl;

import java.util.Base64;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcl.jarvis.common.exception.BizException;
import org.springframework.stereotype.Service;

import com.lcl.jarvis.common.model.PageResult;
import com.lcl.jarvis.admin.modules.person.dto.request.TbPersonImagesReqDTO;
import com.lcl.jarvis.admin.modules.person.dto.response.TbPersonImagesRespDTO;
import com.lcl.jarvis.admin.modules.person.dto.request.TbPersonImagesPageReqDTO;

import com.lcl.jarvis.admin.modules.person.mapper.TbPersonImagesMapper;
import com.lcl.jarvis.admin.modules.person.entity.TbPersonImages;
import com.lcl.jarvis.admin.modules.person.service.ITbPersonImagesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

/**
 * 人员彩超图片存储Service业务层处理
 *
 * @author licl
 * @date 2024-06-26 14:35:51
 */
@Service
@RequiredArgsConstructor
public class TbPersonImagesServiceImpl extends ServiceImpl<TbPersonImagesMapper, TbPersonImages> implements ITbPersonImagesService {

    /**
     * 新增人员彩超图片存储
     *
     * @param body 人员彩超图片存储
     * @return 结果
     */
    @Override
    public Integer add(TbPersonImagesReqDTO body) {
        TbPersonImages entity = BeanUtil.copyProperties(body, TbPersonImages.class);
        return baseMapper.insert(entity);
    }

    /**
     * 批量删除人员彩超图片存储
     *
     * @param ids 需要删除的人员彩超图片存储主键
     * @return 结果
     */
    @Override
    public Integer remove(List<String> ids) {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 修改人员彩超图片存储
     *
     * @param body 人员彩超图片存储
     * @return 结果
     */
    @Override
    public Integer update(TbPersonImagesReqDTO body) {
        TbPersonImages entity = BeanUtil.copyProperties(body, TbPersonImages.class);

        return baseMapper.updateById(entity);
    }


    /**
     * 查询人员彩超图片存储
     *
     * @param id 人员彩超图片存储主键
     * @return 人员彩超图片存储
     */
    @Override
    public TbPersonImagesRespDTO detail(String id) {
        TbPersonImages entity = baseMapper.selectById(id);
        return BeanUtil.copyProperties(entity, TbPersonImagesRespDTO.class);
    }

    /**
     * 查询人员彩超图片存储列表
     *
     * @param req
     * @return 人员彩超图片存储
     */
    @Override
    public PageResult<TbPersonImagesRespDTO> page(TbPersonImagesPageReqDTO req) {
        LambdaQueryWrapper<TbPersonImages> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StrUtil.isNotBlank(req.getPersonId()), TbPersonImages::getPersonId, req.getPersonId());
        queryWrapper.like(StrUtil.isNotBlank(req.getFileName()), TbPersonImages::getFileName, req.getFileName());
        queryWrapper.eq(StrUtil.isNotBlank(req.getFilePath()), TbPersonImages::getFilePath, req.getFilePath());
        queryWrapper.eq(StrUtil.isNotBlank(req.getFileData()), TbPersonImages::getFileData, req.getFileData());
        Page<TbPersonImages> page = baseMapper.selectPage(req.buildPage(), queryWrapper);
        return PageResult.of(page, TbPersonImagesRespDTO.class);
    }

    @Override
    public TbPersonImagesRespDTO uploadImg(MultipartFile file, String personId) {
        if (ObjectUtil.isEmpty(file)) {
            throw  new BizException("文件不能为空");
        }
        try {
            String fileName = file.getOriginalFilename();
            String filePath = "person/" + personId + "/" + fileName;
            byte[] fileData = file.getBytes();
            TbPersonImages entity = new TbPersonImages();
            entity.setPersonId(personId);
            entity.setFileName(fileName);
            entity.setFilePath(filePath);
            //将图片文件转换为base64字符串
            String fileDataStr = Base64.getEncoder().encodeToString(fileData);
            entity.setFileData(fileDataStr);
            baseMapper.insert(entity);

            //上传文件后保存到服务器路径
//            String path = "D:/upload/";
//            String savePath = path + filePath;
//            FileUtil.writeBytes(fileData, savePath);
//            System.out.println("上传成功");
            // 上传成功后，返回文件路径
            TbPersonImagesRespDTO respDTO = BeanUtil.copyProperties(entity, TbPersonImagesRespDTO.class);
            return respDTO;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<TbPersonImagesRespDTO> queryImgByPersonId(String personId) {
        List<TbPersonImages> list = baseMapper.selectList(Wrappers.<TbPersonImages>lambdaQuery().eq(TbPersonImages::getPersonId, personId));
        if (ObjectUtil.isNotEmpty(list)) {
            return BeanUtil.copyToList(list, TbPersonImagesRespDTO.class);
        }
        return null;
    }

}
